#!/usr/bin/python
import os,sys,re

def filename(url):
	if re.search('[^/]+$',url)==None: # file name not found
		raise Exception('%s contains no filename.'%url)
	return re.search('[^/]+$',url).group(0)

def compare(url1, url2): # compare, ignoring case
	return cmp(filename(url1).lower(), filename(url2).lower())

os.chdir('..')
if os.path.exists('urls')==False: raise Exception('"urls" not found.')
urls=open('urls','r')
ulist=[]
for url in urls:
	url=url.strip()
	if len(url)==0: # empty line
		continue
	elif url[0]=='#': # comment line
		raise Exception('I cannot handle comment')
	elif url.find('http://')==0 or url.find('ftp://')==0:
		ulist.append(url)
	else:
		raise Exception('I cannot handle '+url)
urls.close()
ulist.sort(compare)
for i in range(0, len(ulist)-1):
	if ulist[i]==ulist[i+1]:
		raise Exception('Appears two times: '+ulist[i])
urls=open('urls','w')
for url in ulist:
	urls.write(url+'\n')
